package com.sunmi.usercenter.ui.activity.fragment;

import java.lang.reflect.Type;

import org.apache.http.Header;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.sunmi.usercenter.Constant;
import com.sunmi.usercenter.R;
import com.sunmi.usercenter.bean.FmInfo;
import com.sunmi.usercenter.bean.InputToCode;
import com.sunmi.usercenter.bean.Result;
import com.sunmi.usercenter.bean.VerificationcodeBean;
import com.sunmi.usercenter.bean.rpc.ReqCheckUserName;
import com.sunmi.usercenter.bean.rpc.ReqGetVerificationCode;
import com.sunmi.usercenter.ui.component.LoadingDialog;
import com.sunmi.usercenter.utils.Validator;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 项目名称：Sunmi-User-Center
 * 类描述：找回密码的界面
 * 创建人：Abtswiath丶lxy
 * 创建时间：2016/6/7 11:39
 * 修改人：longx
 * 修改时间：2016/6/7 11:39
 * 修改备注：
 */
public class RetrievePsdFragment extends  SuperFragment implements OnClickListener{

	private View mView;
	private TextView mTxtTitle,mTxtError,mBtnNext;
	private EditText mTxtUsername;
	private RelativeLayout mRelBack;
	private LoadingDialog mLoadingDialog;
	private String registerMode;
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initView();
	}
	
	/**
	 * 初始化
	 */
	private void initView() {
		mActivity.SetStatusColorf5f5f5();
		Constant.CLASS_FRAGMENT = Constant.LoginFragment;
		mLoadingDialog = LoadingDialog.createDialog(getActivity(), getString(R.string.verification_phone_ing));
		mTxtTitle = (TextView) mView.findViewById(R.id.title);
		layoutTitle(mTxtTitle);
		mTxtUsername = (EditText) mView.findViewById(R.id.txt_username);
		mTxtError = (TextView) mView.findViewById(R.id.txt_error);
		mBtnNext = (TextView) mView.findViewById(R.id.btn_next);
		mRelBack = (RelativeLayout) mView.findViewById(R.id.back);
		mTxtError.setVisibility(View.INVISIBLE);
		mBtnNext.setOnClickListener(this);
		mRelBack.setOnClickListener(this);
	}
	
	/**
	 * 点击下一步
	 */
	private void clickNext(){
		final String username = mTxtUsername.getText().toString();
		if(Validator.isMobile(username)||Validator.isEmail(username)){
			if(Validator.isMobile(username)){
				registerMode = RegisterFragment.PHONE;
			}else{
				registerMode = RegisterFragment.EMAIL;
			}
			mLoadingDialog.show();
			ReqCheckUserName checkUserName = new ReqCheckUserName();
			checkUserName.setUserName(username);
			mHttpController.checkUsername(checkUserName, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
					String json = new String(arg2);
					Gson gson = new Gson();
					@SuppressWarnings("rawtypes")
					Result result = null;
					try{
						result = gson.fromJson(json, Result.class);
					}catch(Exception e){
						e.printStackTrace();
					}
					
					if (result != null) {
						if (result.code == 1) {
							mLoadingDialog.dismiss();
							mTxtError.setVisibility(View.VISIBLE);
						} else {
							sendCode(username);
							mTxtError.setVisibility(View.INVISIBLE);
						}
					}else{
						showToast(getString(R.string.req_error));
						mLoadingDialog.dismiss();
					}
				}
				@Override
				public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
					showToast(getString(R.string.req_error));
					mLoadingDialog.dismiss();
				}
			});
		}else{
			showToast(getString(R.string.phone_mail_error));
			return;
		}
	}
	
	
	/**
	 * 发送验证码
	 * @param username
	 */
    private void sendCode(final String username){
    	ReqGetVerificationCode reqGetVerificationCode = new ReqGetVerificationCode();
    	reqGetVerificationCode.setUserName(username);
		mHttpController.getVerificationCode4Retrieve(reqGetVerificationCode, new AsyncHttpResponseHandler() {
			
			@Override
			public void onSuccess(int i, Header[] headers, byte[] bytes) {
				String json = new String(bytes);
				Gson gson = new Gson();
				Type type = new TypeToken<Result<VerificationcodeBean>>() {}.getType();
				
				Result<VerificationcodeBean> result = null;
				try {
					result = gson.fromJson(json, type);
				} catch (Exception e) {
					e.printStackTrace();
				}
				if(result!=null){
					if(result.code==1){
//						VerificationcodeBean verificationcodeBean = result.getData();
//						InputToCode inputToCode = new InputToCode();
//						Constant.mInputToCode.setCode(verificationcodeBean.getCode());
						Constant.mInputToCode.setSelectMode(registerMode);
						Constant.mInputToCode.setUsername(username);
						Constant.mInputToCode.setFramgment(2);
						openFM(new FmInfo(InputCodeFragment.class, Constant.mInputToCode));
					}else if(result.code==160038){
						showToast(getString(R.string.code_send_failed));
					}else if(result.code==160042){
						showToast(getString(R.string.the_address_error_to_phone));
					}else if(result.code==160039||result.code==160040||result.code==160041){
						showToast(getString(R.string.phone_get_code_max_error));
					}else{
						showToast(getString(R.string.code_send_failed));
					}
				}else{
					showToast(getString(R.string.get_code_error));
				}
				mLoadingDialog.dismiss();
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				showToast(getString(R.string.req_error));
				mLoadingDialog.dismiss();
			}
		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		return mView = inflater.inflate(R.layout.fragment_retrieve_password,container,false);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			openFM(new FmInfo(LoginFragment.class,null));
			break;
		case R.id.btn_next:
			clickNext();
			break;
		default:
			break;
		}
	}
	
}
